#include "RootcontactItem.h"

RootcontactItem::RootcontactItem(bool hasArrow, QWidget *parent)
	: QLabel(parent),
	m_hasArrow(hasArrow),
	m_rotation(0)
{
	setFixedHeight(32);
	setSizePolicy(QSizePolicy::Policy::Expanding, QSizePolicy::Policy::Expanding);

	m_animation = new QPropertyAnimation(this, "rotation");
	m_animation->setDuration(50);
	m_animation->setEasingCurve(QEasingCurve::Type::OutCubic);

}

RootcontactItem::~RootcontactItem()
{
}

void RootcontactItem::setText(const QString & title)
{
	m_titleText = title;
	//update();
}

void RootcontactItem::setExpanded(bool expand)
{
	if (expand) {
		m_animation->setEndValue(90);

	}
	else
	{
		m_animation->setEndValue(0);

	}

	m_animation->start();
}

int RootcontactItem::rotation()
{
	return m_rotation;
}

void RootcontactItem::setRotation(int rotation)
{
	m_rotation = rotation;
	update();
}

void RootcontactItem::paintEvent(QPaintEvent * event)
{
	//() << "RootcontactItem::paintEvent";
	QPainter painter(this);

	painter.setRenderHint(QPainter::RenderHint::TextAntialiasing);
	QFont font;
	font.setPointSize(10);
	painter.setFont(font);
	painter.drawText(24, 0,
		width() - 24, height(),
		Qt::AlignLeft | Qt::AlignVCenter,
		m_titleText);

	painter.setRenderHint(QPainter::RenderHint::SmoothPixmapTransform);
	painter.save();

	if (m_hasArrow) {
		QPixmap pixmap(":/Resources/MainWindow/arrow.png");

		QPixmap tmpPixmap(pixmap.size());
		tmpPixmap.fill(Qt::GlobalColor::transparent);

		QPainter p(&tmpPixmap);
		p.setRenderHint(QPainter::RenderHint::SmoothPixmapTransform);

		p.translate(pixmap.width() / 2, pixmap.height() / 2);
		p.rotate(m_rotation);
		p.drawPixmap(0 - pixmap.width() / 2, 0 - pixmap.height() / 2, pixmap);

		painter.drawPixmap(6, (height() - pixmap.height()) / 2, tmpPixmap);

		painter.restore();
	}

	return QLabel::paintEvent(event);
}
